package com.iotproject.forumservice.service.impl;

import com.iotproject.commonutils.forumvo.UcenterMemberForum;
import com.iotproject.forumservice.client.UcenterClient;
import com.iotproject.forumservice.entity.ForumQuestion;
import com.iotproject.forumservice.entity.vo.QuestionVo;
import com.iotproject.forumservice.mapper.ForumQuestionMapper;
import com.iotproject.forumservice.service.ForumQuestionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 论坛问题 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-04-09
 */
@Service
public class ForumQuestionServiceImpl extends ServiceImpl<ForumQuestionMapper, ForumQuestion> implements ForumQuestionService {

    @Autowired
    UcenterClient ucenterClient;

    @Override
    public Map<String, Object> getAllListByPage(Integer page, Integer size) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<ForumQuestion> allListByPage = baseMapper.getAllListByPage(page, size);
        Integer total = baseMapper.selectCount(null);

        //传给前端的列表
        List<QuestionVo> resultList = new ArrayList<>();
        for (int i = 0; i < allListByPage.size(); i++) {
            ForumQuestion forumQuestion = allListByPage.get(i);
            QuestionVo questionVo = new QuestionVo();
            BeanUtils.copyProperties(forumQuestion, questionVo);
            //截取问题简介
            if (questionVo.getDescription().length() >= 30) {
                questionVo.setDescription(questionVo.getDescription().substring(0, 30) + "...");
            }
            //获取用户信息
            String creatorId = questionVo.getCreatorId();
            Map<String, String> userInfo = getUserInfo(creatorId);
            questionVo.setNickname(userInfo.get("nickName"));
            questionVo.setAvatar(userInfo.get("avatar"));
            questionVo.setSign(userInfo.get("sign"));

            resultList.add(questionVo);
        }

        Map<String, Object> maps = new HashMap<>();
        maps.put("list", resultList);
        maps.put("total", total);
        return maps;
    }

    @Override
    public QuestionVo getQuestionById(String questionId) {
        //增加阅读数
        baseMapper.increaseViewCount(questionId);
        ForumQuestion question = baseMapper.selectById(questionId);
        QuestionVo questionVo = new QuestionVo();
        BeanUtils.copyProperties(question, questionVo);
        Map<String, String> creatorInfo = getUserInfo(questionVo.getCreatorId());
        questionVo.setNickname(creatorInfo.get("nickname"));
        questionVo.setAvatar(creatorInfo.get("avatar"));
        questionVo.setSign(creatorInfo.get("sign"));
        return questionVo;
    }

    @Override
    //根据id获取用户信息(昵称、头像、签名)
    public Map<String, String> getUserInfo(String userId) {
        UcenterMemberForum userInfoForum = ucenterClient.getUserInfoForum(userId);
        Map<String, String> userInfo = new HashMap<>();
        userInfo.put("nickname", userInfoForum.getNickname());
        userInfo.put("avatar", userInfoForum.getAvatar());
        userInfo.put("sign", userInfoForum.getSign());
        return userInfo;
    }

    @Override
    public void increaseCommentCount(String questionId) {
        baseMapper.increaseCommentCount(questionId);
    }

    @Override
    public void increaseLikeCount(String questionId) {
        baseMapper.increaseLikeCount(questionId);
    }

    @Override
    public void decreaseLikeOperator(String questionId) {
        baseMapper.decreaseLikeOperator(questionId);
    }

    @Override
    public List<ForumQuestion> relatedQuestion(String questionId, String tags) {
        List<ForumQuestion> relatedQuestions = baseMapper.relatedQuestion(questionId, tags);
        return relatedQuestions;
    }

    @Override
    public void updateQuestion(String id, String title, String description, String tag) {
        baseMapper.updateQuestion(id, title, description, tag);
    }

    @Override
    public List<ForumQuestion> hotTopic(String condition) {
        List<ForumQuestion> forumQuestions = baseMapper.hotTopic(condition);
        return forumQuestions;
    }

    @Override
    public Map<String, Object> questionListByKeyWords(String keyWords, Integer page, Integer size) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        //加上 % 用作条件查询
//        List<ForumQuestion> allListByPage = baseMapper.questionListByKeyWords(keyWords + "%", page, size);
//        QueryWrapper<ForumQuestion> forumQuestionQueryWrapper = new QueryWrapper<>();
//        forumQuestionQueryWrapper.likeLeft("title", keyWords).or().likeLeft("description", keyWords);
//        Integer total = baseMapper.selectCount(forumQuestionQueryWrapper);

        List<ForumQuestion> allListByPage = baseMapper.questionListByKeyWords("%" + keyWords + "%", page, size);
        QueryWrapper<ForumQuestion> forumQuestionQueryWrapper = new QueryWrapper<>();
        forumQuestionQueryWrapper.like("title", keyWords).or().like("description", keyWords);
        Integer total = baseMapper.selectCount(forumQuestionQueryWrapper);

        //传给前端的列表
        List<QuestionVo> resultList = new ArrayList<>();
        for (int i = 0; i < allListByPage.size(); i++) {
            ForumQuestion forumQuestion = allListByPage.get(i);
            QuestionVo questionVo = new QuestionVo();
            BeanUtils.copyProperties(forumQuestion, questionVo);
            //截取问题简介
            if (questionVo.getDescription().length() >= 30) {
                questionVo.setDescription(questionVo.getDescription().substring(0, 30) + "...");
            }
            //获取用户信息
            String creatorId = questionVo.getCreatorId();
            Map<String, String> userInfo = getUserInfo(creatorId);
            questionVo.setNickname(userInfo.get("nickName"));
            questionVo.setAvatar(userInfo.get("avatar"));
            questionVo.setSign(userInfo.get("sign"));

            resultList.add(questionVo);
        }

        Map<String, Object> maps = new HashMap<>();
        maps.put("list", resultList);
        maps.put("total", total);
        return maps;
    }

    @Override
    public Map<String, Object> questionListByUserId(String userId, Integer page, Integer size) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        //加上 % 用作条件查询
        List<ForumQuestion> allListByPage = baseMapper.questionListByUserId(userId, page, size);
        QueryWrapper<ForumQuestion> forumQuestionQueryWrapper = new QueryWrapper<>();
        forumQuestionQueryWrapper.eq("creator_id", userId);
        Integer total = baseMapper.selectCount(forumQuestionQueryWrapper);

        //传给前端的列表
        List<QuestionVo> resultList = new ArrayList<>();
        for (int i = 0; i < allListByPage.size(); i++) {
            ForumQuestion forumQuestion = allListByPage.get(i);
            QuestionVo questionVo = new QuestionVo();
            BeanUtils.copyProperties(forumQuestion, questionVo);
            //截取问题简介
            if (questionVo.getDescription().length() >= 30) {
                questionVo.setDescription(questionVo.getDescription().substring(0, 30) + "...");
            }
            //获取用户信息
            String creatorId = questionVo.getCreatorId();
            Map<String, String> userInfo = getUserInfo(creatorId);
            questionVo.setNickname(userInfo.get("nickName"));
            questionVo.setAvatar(userInfo.get("avatar"));
            questionVo.setSign(userInfo.get("sign"));

            resultList.add(questionVo);
        }

        Map<String, Object> maps = new HashMap<>();
        maps.put("list", resultList);
        maps.put("total", total);
        return maps;
    }


}
